<?php

namespace App\Services;

use App\Exceptions\FireFlyException;

class WxPayService 
{
    public static function h5Pay($order_sn,$description,$amount,$ip,$notifyUrl){
        $api = app('easywechat.pay')->getClient();
        $data = [
                    "appid" => config('easywechat.pay.default.app_id'),
                    "mchid" => config('easywechat.pay.default.mch_id'),
                    "description" => $description,
                    "out_trade_no" => $order_sn,
                    "time_expire" => date('Y-m-d\\TH:i:sP',time() + 600),
                    "notify_url" => $notifyUrl,
                    "amount" => [
                        "total" => (int) ($amount * 100)
                    ],
                    "scene_info" => [
                        "payer_client_ip" => $ip,
                        "h5_info" => [
                            "type" => "Wap" // 示例值：iOS, Android, Wap
                        ],
                    ],
                ];
            // dd($data);
        $response = $api->postJson('/v3/pay/transactions/h5', $data);
        // 获取响应内容
        $result = $response->toArray(false); // 失败不抛出异常
        // 判断是否有异常
        if (isset($result['code']) && $result['code'] != 'SUCCESS') {
            throw new FireFlyException(403,"调起微信支付失败，".$result['message']);
        }
        return $result;
    }
}